package com.lkd.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.lkd.dao.ChannelDao;
import com.lkd.http.controller.vo.ChannelConfigReq;
import com.lkd.entity.ChannelEntity;
import com.lkd.entity.SkuEntity;
import com.lkd.exception.LogicException;
import com.lkd.service.ChannelService;
import com.lkd.service.SkuService;
import com.lkd.vo.ChannelVO;
import com.lkd.vo.SkuVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
/**
 * <p>
 * 售货机货道表 服务类
 * </p>
 *
 * @author LKD
 */
@Service
public class ChannelServiceImpl extends ServiceImpl<ChannelDao, ChannelEntity> implements ChannelService {

    @Autowired
    private SkuService skuService;


    /*
    * 货道列表
    * */
    @Override
    public List<ChannelEntity> channelList(String innercode) {
        //业务校验
        if(StrUtil.isEmpty(innercode)){
            throw new LogicException("参数非法");
        }
        //1.根据设备编号查询货道列表
        LambdaQueryWrapper<ChannelEntity> qw = new LambdaQueryWrapper<>();
        qw.eq(ChannelEntity::getInnerCode,innercode);
        List<ChannelEntity> list = this.list(qw);
        //2.返回
        return list;
    }

    /*
    * 货道配置
    * */
    @Override
    public void channelConfig(ChannelConfigReq channelConfigReq) {
       //1. 遍历前端提交的货道List
        for (ChannelEntity channelReq : channelConfigReq.getChannelList()) {
            //2.根据innerCode+channelCode查询货道信息
            LambdaQueryWrapper<ChannelEntity> qw = new LambdaQueryWrapper<>();
            qw.eq(ChannelEntity::getInnerCode,channelConfigReq.getInnerCode());
            qw.eq(ChannelEntity::getChannelCode,channelReq.getChannelCode());
            ChannelEntity channelDb = this.getOne(qw);
            //3.判断前端提供货道是否有商品id
            if(channelReq.getSkuId()==0){
                //3-1 如果没有，删除旧的视频信息(设置为0)
                channelDb.setSkuId(0L);
                channelDb.setPrice(0);
            }else {
                //3-2 如果有，将新商品id和价格设置到货道(根据skuId查询新的商品信息)
                SkuEntity skuEntity = skuService.getById(channelReq.getSkuId());
                channelDb.setSkuId(skuEntity.getSkuId());
                channelDb.setPrice(skuEntity.getPrice());
            }
            //4. 更新数据库
            this.updateById(channelDb);
        }
    }

    /*
    * 商品在售货机中所在货道
    * */
    @Override
    public ChannelVO hasCapacity(String innerCode, Long skuId) {
        //1. 先查询货道
        List<ChannelEntity> channelList = this.lambdaQuery()
                .eq(ChannelEntity::getInnerCode, innerCode)
                .eq(ChannelEntity::getSkuId, skuId)
                .gt(ChannelEntity::getCurrentCapacity, 0)//库存>0
                .list();
        //2. 取出其中一个
        if(CollUtil.isEmpty(channelList)){
            return null;
        }
        ChannelEntity channel = channelList.stream().findFirst().get();
        //3. 转换vo
        ChannelVO channelVO = BeanUtil.copyProperties(channel, ChannelVO.class);

        //3-1 查询商品
        SkuEntity sku = skuService.getById(channel.getSkuId());
        SkuVO skuVO = BeanUtil.copyProperties(sku, SkuVO.class);
        skuVO.setImage(sku.getSkuImage());//图片
        skuVO.setRealPrice(sku.getPrice());//真实售价
        skuVO.setCapacity(channel.getCurrentCapacity());//商品余量
        channelVO.setSkuVO(skuVO);
        //4.返回
        return channelVO;
    }
}
